package car;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class nannv {

    public static class MyMapper extends Mapper<Object, Text, Text, IntWritable> {
        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            String line = new String(value.getBytes(), 0, value.getLength(), "GBK");
            String[] strs = line.trim().split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);

            if (strs != null && strs.length > 0) {
                String gender = strs[strs.length - 1];
                if ("男性".equals(gender) || "女性".equals(gender)) {
                    word.set(gender);
                    context.write(word, one);
                }
            }
        }
    }

    public static class MyReducer extends Reducer<Text, IntWritable, Text, DoubleWritable> {
        private Map<Text, Long> map = new HashMap<>();
        private long total = 0;

        @Override
        public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            long sum = 0;
            for (IntWritable val : values) {
                sum += val.get();
            }
            total += sum;
            map.put(new Text(key), sum);
        }

        public void cleanup(Context context) throws IOException, InterruptedException {
            Set<Text> ks = map.keySet();
            for (Text t : ks) {
                DoubleWritable result = new DoubleWritable((double) map.get(t) / total);
                context.write(new Text(t.toString() + " count"), new DoubleWritable(map.get(t).intValue()));
                context.write(new Text(t.toString() + " ratio"), result);
            }
        }
    }

    public static void main(String args[]) throws Exception {
        String namenode_ip = "192.168.128.130";
        String hdfs = "hdfs://" + namenode_ip + ":8020";
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", hdfs);

        String jobName = "gender_analysis";
        Job job = Job.getInstance(conf, jobName);
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);
        job.setReducerClass(MyReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(DoubleWritable.class);

        String dataDir = "/car/data.csv";
        String outputDir = "/Outputche/v2";
        Path inPath = new Path(hdfs + dataDir);
        Path outPath = new Path(hdfs + outputDir);
        FileInputFormat.addInputPath(job, inPath);
        FileOutputFormat.setOutputPath(job, outPath);

        FileSystem fs = FileSystem.get(conf);
        if (fs.exists(outPath)) {
            fs.delete(outPath, true);
        }

        System.out.println("Job: " + jobName + " is running...");
        if (job.waitForCompletion(true)) {
            System.out.println("success!");
        } else {
            System.out.println("failed!");
            System.exit(1);
        }
    }
}
